import {
  getCategoryList,
  getGiftList,
  getModelList,
  getProductListByCategory,
  putlv
} from '@/base/api/LvApi';

export class FormBloc {
  model: any = {
    lv: 0,
    upgradeCondition: 0,
    name: '',
    upgradeGift: 0,
    upgradePoint: 0,
    memberCard: ''
  };
  editModel: any = {
    categoryId: '0',
    modelId: '0',
    productId: '0',
    num: 1,
    exp: 1,
    a_sortName: '',
    b_modelName: '',
    c_ProductNumber: '',
    imgUrl: ''
  };
  loading = false;
  fileList: Array<any> = [];
  num: number = 0;
  condition:any = [];
  conditionList = [
    {'id': 0, 'conditionname': '会员注册'},
    {'id': 1, 'conditionname': '成长值'}
  ];
  gift:any = [];
  giftList = [
    {'id': 1, 'giftname': '送积分'},
    {'id': 2, 'giftname': '送实物'}
    ];
  tempArr:any;
  thingList:any = [];
  isDetail = false;
  a = {items: []};
  b = {items: []};
  c = {items: []};

  getGift(lv) {
    this.loading = true;
    return getGiftList(lv).then((res) => {
      this.thingList.push({
        'giftId': res.data.giftId,
        'sortName': res.data.sortName,
        'modelName': res.data.modelName,
        'productNumber': res.data.productNumber,
        'num': res.data.num,
        'exp': res.data.exp
      });
    }).finally(() => {
      this.loading = false;
    });
  }
  getCategory() {
    this.loading = true;
    return getCategoryList().then((res) => {
      // @ts-ignore
      this.a.items = res.data;
      // this.categoryList = res.data;
      return this.a;
    }).finally(() => {
      this.loading = false;
    });
  }
  getAllModel(value) {
    this.loading = true;
    return getModelList({categoryId: value}).then((res) => {
      // @ts-ignore
      this.b.items = res.data;
      // this.modelList = res.data;
      return this.b;
    }).finally(() => {
      this.loading = false;
    });
  }
  getAllProduct(value) {
    this.loading = true;
    return getProductListByCategory({modelId: value}).then((res) => {
      // @ts-ignore
      this.c.items = res.data;
      // this.productList = res.data;
      return this.c;
    }).finally(() => {
      this.loading = false;
    });
  }
  getCategoryName(id) {
    // @ts-ignore
    let item = this.a.items.find(item => item.categoryId === id);
    // @ts-ignore
    return item == null ? '' : item.sortName;
  }
  getModelName(id) {
    // @ts-ignore
    let item = this.b.items.find(item => item.modelId === id);
    // @ts-ignore
    return item == null ? '' : item.modelName;
  }
  getProductName(id) {
    // @ts-ignore
    let item = this.c.items.find(item => item.productId === id);
    if (item !== null) {
      // @ts-ignore
      this.editModel.imgUrl = item.imgUrl;
    }
    // @ts-ignore
    return item == null ? '' : item.productNumber;
  }
  getThelement(id) {
    return this.thingList.find(item => item.giftId === id);
  }
  getCategoryId(value) {
    // @ts-ignore
    let item = this.a.items.find(item => item.sortName === value);
    // @ts-ignore
    return item == null ? '' : item.categoryId;
  }
  getModelId(value, res) {
      // @ts-ignore
      let item = res.items.find(item => item.modelName === value);
      // @ts-ignore
      return item == null ? '' : item.modelId;
  }
  getProductId(value, res) {
    // @ts-ignore
    let item = res.items.find(item => item.productNumber === value);
    if (item !== null) {
      // @ts-ignore
      this.editModel.imgUrl = item.imgUrl;
    }
    // @ts-ignore
    return item == null ? '' : item.productId;
  }
  save(model:any) {
    this.loading = true;
    return putlv({
      lv: model.lv, // 会员等级
      upgradeCondition: model.upgradeCondition, // 会员升级条件，
      name: model.name, // 等级名称
      upgradeGift: model.upgradeGift, // 送实物1，不送0
      upgradePoint: model.upgradePoint, // 非必传，当没勾选时
      memberCard: model.memberCard,
      sortName: this.editModel.a_sortName,
      modelName: this.editModel.b_modelName,
      productNumber: this.editModel.c_ProductNumber,
      num: this.editModel.num,
      // exp: this.editModel.exp,
      imgUrl: this.editModel.imgUrl
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }
}
